<template>
  <view class="transfer">
    <topTitle :title="$t('transfer.transfer')" />
    <view class="com">
      <view class="title">{{ $t('transfer.transferSelection') }}</view>
      <view class="list">
        <view v-for="(item, idx) in UserSwitchTypeArr" :key="idx" @click="switch_type = item.switch_type"
          :class="{ avtive: item.switch_type == switch_type }">
          <view>
            <view>{{ item.balance1 }}</view>
            <view>{{ item.name1 }}</view>
          </view>
          <view>
            <view>{{ item.balance2 }}</view>
            <view>{{ item.name2 }}</view>
          </view>
        </view>
      </view>
      <view class="title">{{ $t('transfer.transferAmount') }}</view>
      <up-input :placeholder="$t('transfer.enterTransferAmount')" type="number" v-model="money" clearable class="item" border="none" />
      <up-button :text="$t('transfer.confirm')" @click="saveFn"></up-button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance } from "vue";
const instance = getCurrentInstance()?.appContext.config.globalProperties;
import { UserSwitchType, UserMoney } from "@/api/UserApi";
let UserSwitchTypeArr = ref<Array<any>>([]);
let switch_type = ref<number>(1);
let money = ref<any>();
import { onShow } from "@dcloudio/uni-app";

const saveFn = () => {
  if (!money.value) {
    uni.showToast({
      title: instance?.$t('transfer.enterAmountPrompt'),
      icon: "none"
    });
    return;
  }
 instance?.goTo(`/pages/setUp/enterPsw?type=transfer&title=${instance?.$t('transfer.transferSuccess')}&money=${money.value}&switch_type=${switch_type.value}`);
};

onShow(async () => {
  money.value = '';
  await UserMoney();
  UserSwitchType().then((e) => {
    let UserMoneyObj = uni.getStorageSync("UserMoney");
    e.forEach((item: any) => {
      if (item.out_account == 1) {
        item['name1'] = instance?.$t('transfer.profitAccount');
        item['balance1'] = UserMoneyObj.balance;
      }
      if (item.out_account == 3) {
        item['name1'] = instance?.$t('transfer.wisdomAccount');
        item['balance1'] = UserMoneyObj.balance_wisdom;
      }
      if (item.in_account == 2) {
        item['name2'] = instance?.$t('transfer.quantifyAccount');
        item['balance2'] = UserMoneyObj.balance_quantify;
      }
      if (item.in_account == 3) {
        item['name2'] = instance?.$t('transfer.wisdomAccount');
        item['balance2'] = UserMoneyObj.balance_wisdom;
      }
    });
    UserSwitchTypeArr.value = e;
  });
});
</script>

<style scoped lang="scss">
@import "../../static/scss/mine.scss";
</style>